package com.resourcestore.action;

import java.io.UnsupportedEncodingException;

import java.text.ParseException;
import java.util.List;

import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;
import com.resourcestore.action.BaseAction;
import com.resourcestore.dao.StudentsDao;
import com.resourcestore.model.Students;

public class StudentsAction extends BaseAction {

	private Students students;
	private List<Students> listStudents;
	private StudentsDao studentsDao = new StudentsDao();
	private Pagination pagination = new Pagination();

	/**
	 * 查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */

	public String listStudents() {
		students = new Students();
		pagination.setTotal(studentsDao.queryStudentsNum(students));
		listStudents = studentsDao.selectStudentsPage(students, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listStudents.size(); i++) {
			try {
				listStudents.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listStudents.get(i)
								.getGmtCreated()));
				listStudents.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listStudents.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return "default";
	}

	/**
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 * 
	 */
	public String searchListStudents() {

		// 对内容进行urldecoder解码
		try {
			students.setStuNum(new String(students.getStuNum().getBytes(
					"iso-8859-1"), "UTF-8"));
			students.setStuName(new String(students.getStuName().getBytes(
					"iso-8859-1"), "UTF-8"));
			students.setClassName(new String(students.getClassName().getBytes(
					"iso-8859-1"), "UTF-8"));
			students.setMajorName(new String(students.getMajorName().getBytes(
					"iso-8859-1"), "UTF-8"));
			students.setDepartmentName(new String(students.getDepartmentName()
					.getBytes("iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(studentsDao.queryStudentsNum(students));
		listStudents = studentsDao.selectStudentsPage(students, pagination);

		// 将bigint数字处理为时间格式
		for (int i = 0; i < listStudents.size(); i++) {
			try {
				listStudents.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listStudents.get(i)
								.getGmtCreated()));
				listStudents.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listStudents.get(i)
								.getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/Students/listStudents.jsp");
	}

	/**
	 * 预插入数据
	 */
	public String preInsertStudents() {

		// 为配合级联表单验证赋初始值
		// students = new Students();
		// students.setMajorGroupId(0);
		// students.setMajorId(0);
		// students.setClassId(0);

		return newpath("/Students/insertStudents.jsp");
	}

	/**
	 * 插入一条数据
	 */
	public String insertStudents() {

		try {
			students.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			students.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		studentsDao.insertStudents(students);
		return redirect(this.getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	public String preUpdateStudents() {
		students = studentsDao.selectStudentsById(students);

		return newpath("/Students/updateStudents.jsp");
	}

	/**
	 * 更新信息
	 * 
	 */
	public String updateStudents() {

		try {
			// students.setGmtCreated(CommonUtil.getSysTimeFormatNum());
			students.setGmtModified(CommonUtil.getSysTimeFormatNum());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		studentsDao.updateStudents(students);
		return redirect(this.getUrl());
	}

	/**
	 * 删除信息
	 */
	public String deleteStudents() {
		studentsDao.deleteStudents(students);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreStudents() {
	
		studentsDao.deleteMoreStudents(students);
		return redirect(this.getUrl());
	}

	public Students getStudents() {
		return students;
	}

	public void setStudents(Students students) {
		this.students = students;
	}

	public List<Students> getListStudents() {
		return listStudents;
	}

	public void setListStudents(List<Students> listStudents) {
		this.listStudents = listStudents;
	}

	public Pagination getPagination() {
		return this.pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
}
